package com.jxl_dyw.UI;

import com.jxl_dyw.entity.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

public class RegisterUI extends JFrame {
    public RegisterUI() {
        // 设置窗口标题和关闭行为
        setTitle("注册");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 关闭时只关闭当前窗口，不退出整个程序
        setSize(800, 600); // 增大窗口尺寸
        setLocationRelativeTo(null); // 居中显示窗口

        // 添加窗口监听事件，点击关闭按钮时回退到登录页面
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                new LoginUI().setVisible(true);
            }
        });

        // 使用 GridBagLayout 进行布局
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10); // 增大组件之间的间距

        // 创建一个面板用于存放所有输入字段
        JPanel inputPanel = new JPanel(new GridBagLayout());
        gbc.gridx = 0;
        gbc.gridy = 0;

        // 设置全局字体，使所有组件的文字更大
        Font font = new Font("宋体", Font.PLAIN, 18);

        // 创建用户名和密码组件并添加到面板
        JLabel userLabel = new JLabel("用户名:");
        JTextField userField = new JTextField(20);
        userLabel.setFont(font);
        userField.setFont(font);
        addComponent(inputPanel, userLabel, userField, gbc);

        JLabel passwordLabel = new JLabel("密码:");
        JTextField passwordField = new JTextField(20);
        passwordLabel.setFont(font);
        passwordField.setFont(font);
        addComponent(inputPanel, passwordLabel, passwordField, gbc);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        JButton registerButton = new JButton("注册");
        registerButton.setFont(font); // 设置按钮字体
        buttonPanel.add(registerButton);

        // 添加按钮点击事件监听器
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取输入的用户信息
                String username = userField.getText().trim();
                String password = passwordField.getText().trim();

                // 验证输入是否为空
                if (username.isEmpty() || password.isEmpty()) {
                    JOptionPane.showMessageDialog(RegisterUI.this, "用户名和密码不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                // 创建 User 对象
                User user = new User(username, password);

                // 将 User 对象添加到哈希表中，以用户名作为键
                LoginUI.getUserMap().put(user.getUsername(), user.getPassword());

                // 将用户信息写入文件
                try {
                    LoginUI.writeUsersToFile(LoginUI.filePath);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }

                // 清空输入框
                userField.setText("");
                passwordField.setText("");

                // 弹出对话框
                JOptionPane.showMessageDialog(RegisterUI.this, "注册成功", "提示", JOptionPane.INFORMATION_MESSAGE);

                // 关闭注册页面并重新显示登录页面
                dispose();
                new LoginUI().setVisible(true);
            }
        });

        // 将输入面板和按钮面板添加到主窗口
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2; // 占两列
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        add(inputPanel, gbc);

        gbc.gridy++;
        gbc.gridwidth = 1; // 占一列
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.weighty = 0.1;
        add(buttonPanel, gbc);

        // 设置窗口可见
        setVisible(true);
    }

    private void addComponent(JPanel panel, JLabel label, JComponent textField, GridBagConstraints gbc) {
        gbc.gridx = 0;
        gbc.gridy++;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(label, gbc);

        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(textField, gbc);
    }
}